package com.paw.demo;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

/**
 * @author lvzihai
 * @date 2022/4/15
 **/
public class LotteryA {


  private static int draw(){
    int totalCount = 10000;
    int pa = 10;
    int pb = 20;
    int pc = 30;
    int pd = totalCount -pa-pb-pc;
    int[] gifts = new int[]{pa,pb,pc,pd};

    int[] giftRange = new int[gifts.length];
    int sum = 0;
    for (int i = 0; i < gifts.length; i++) {
      sum+=gifts[i];
      giftRange[i] = sum;
    }
    StrUtil s;

    int randomVal = RandomUtil.randomInt(totalCount);
    for (int i = 0; i < giftRange.length; i++) {
      if(randomVal<giftRange[i]){
        return i;
      }
    }

    return -1;
  }

  public static void main (String[] args) {
    Map<Integer,Integer> map = new HashMap<>();
    int totalCount = 100000;
    for (int i = 0; i < totalCount; i++) {
      int index = draw();
      Integer val = map.get(index);
      val = val==null?1:val+1;
      map.put(index,val);
    }
    for (Entry<Integer, Integer> entry : map.entrySet()) {
      System.out.println(entry.getKey()+" "+10000.0*entry.getValue()/totalCount);
    }
  }


}
